﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using FastJSON;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using DingTida.Models;
using RouteAttribute = Microsoft.AspNetCore.Components.RouteAttribute;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Data.SqlClient;
using Microsoft.IdentityModel.Protocols;
using System.Data;
using System.IO;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.Internal;
using Microsoft.AspNetCore.Hosting;
using System.Globalization;
using DingTida.IBLL;

namespace DingTida.BLL
{
    public  class WorkFlowInstance : IWorkFlowInstance
    {
        public EasyStarContext db;
        public IMsg msg;
        public WorkFlowInstance(EasyStarContext _db,IMsg _msg)
        {
            this.db = _db;
            this.msg = _msg;
        }
        #region   指定人员审批签核

        public int  AddInstanceByUser(int id ,string user,string flowid )
        {

          

            WorkFlowsInstance wfii = new WorkFlowsInstance();
            wfii.WorkFlowId = flowid;
            wfii.FlowStep = id;
            wfii.ApproveUser = user;
            wfii.Approved = false;
            wfii.Refused = false;
            wfii.LastStep = false;
            wfii.IsCurrentStep=false;

            db.WorkFlowsInstance.Add(wfii);


            db.SaveChanges();
            return id + 1;

        }

        #endregion

        #region   指定人员终审

        public int AddInstanceByUserLast(int id, string user, string flowid)
        {



            WorkFlowsInstance wfii = new WorkFlowsInstance();
            wfii.WorkFlowId = flowid;
            wfii.FlowStep = id ;
            wfii.ApproveUser = user;
            wfii.Approved = false;
            wfii.Refused = false;
            wfii.LastStep = true;
            wfii.IsCurrentStep=false;

            db.WorkFlowsInstance.Add(wfii);


            db.SaveChanges();
            return id + 1;

        }

        #endregion


        #region 同部门的经理签核
        public int AddInstanceByManager( string user, string flowid)
        {

            var staff = db.EmEmpData.Where(c => c.EmpNo == user).FirstOrDefault();
            var dept = db.EsDept.Where(c => c.FullCode == staff.Dept).FirstOrDefault();
            WorkFlowsInstance wfii = new WorkFlowsInstance();
            wfii.WorkFlowId = flowid;
            wfii.FlowStep = 1;
            wfii.ApproveUser = dept.Manager;
            wfii.Approved = false;
            wfii.Refused = false;
            wfii.LastStep = false;
            wfii.IsCurrentStep=true;        
            msg.SendMsg("钉钉提示您，泰德小程序内OA项目 有一项待您核签，", dept.Manager);
            db.WorkFlowsInstance.Add(wfii);
            db.SaveChanges();
            return 1;

        }
        #endregion
        public void AddInstanceByTopManager(string user, string flowid)
        {

            WorkFlowsInstance wfii = new WorkFlowsInstance();
            wfii.WorkFlowId = flowid;
            wfii.FlowStep =  1;
            wfii.ApproveUser = user;
            wfii.Approved = false;
            wfii.Refused = false;
            wfii.LastStep = false;
            wfii.IsCurrentStep = true;



            msg.SendMsg("钉钉提示您，泰德小程序内OA项目 有一项待您核签，", user);

            db.WorkFlowsInstance.Add(wfii);


            db.SaveChanges();
         

        }
        #region 人事审批


        public void AddInStanceOfHR(int id,string flowid)
        {

            var hrid = db.SysInfo.FirstOrDefault().Telphone;

            var hrlist = db.EmEmpData.Where(c => c.Dept == hrid && c.LzDate == null).ToList();
            var i = hrlist.Count();

            for (int a = 0; a < i; a++)
            {
                WorkFlowsInstance wfii = new WorkFlowsInstance();
                wfii.WorkFlowId = flowid;
                wfii.FlowStep = id ;
                wfii.ApproveUser = hrlist[a].EmpNo;
                wfii.Approved = false;
                wfii.Refused = false;
                wfii.LastStep = true;
                wfii.IsCurrentStep=false;


                db.WorkFlowsInstance.Add(wfii);


                db.SaveChanges();


            }
        
        
        }
        #endregion

    }
}
